package top.minuy.structure.tree.trie;

import top.minuy.util.helper.FileHelper;
import top.minuy.structure.linear.array.Array;
import top.minuy.structure.set.BSTSet;
import top.minuy.structure.set.Set;

/**
 * @author Minuy
 * @time 20:07
 * @date 2021/10/5
 */
public class Test {
    public static void main(String[] args) {
        double d1,d2;
        Trie trie;
        BSTSet<String> bstSet;

//        trie = new Trie();
//        trie.add("add");

        System.out.println();
        System.out.println("-> Trie");
        trie = new Trie();
        d1=testTrie(trie,"A Tale of Two Cities","src/top.minuy.res/A_Tale_of_Two_Cities.txt");
        System.out.println();
        trie = new Trie();
        d2=testTrie(trie,"Pride and Prejudice","src/top.minuy.res/Pride_and_Prejudice.txt");
        System.out.println();
        System.out.println(String.format("Time : %f s, %f s",d1,d2));

        System.out.println();
        System.out.println("-> BSTSet");
        bstSet = new BSTSet<>();
        d1=testSet(bstSet,"A Tale of Two Cities","src/top.minuy.res/A_Tale_of_Two_Cities.txt");
        System.out.println();
        bstSet = new BSTSet<>();
        d2=testSet(bstSet,"Pride and Prejudice","src/top.minuy.res/Pride_and_Prejudice.txt");
        System.out.println();
        System.out.println(String.format("Time : %f s, %f s",d1,d2));
    }

    private static double testSet(Set<String> set, String title, String fileName){

        System.out.println(title);

        Array<String> array = new Array<>();

        FileHelper.readFile(fileName,array);

        System.out.println("Total words : "+array.getSize());

        long startTime = System.nanoTime();
        for (int i=0;i<array.getSize();i++) {
            set.add(array.get(i));
        }
        long endTime = System.nanoTime();

        System.out.println(String.format("Total different words : custom = %d",set.getSize()));

        return (endTime-startTime)/1000000000.0;
    }

    private static double testTrie(Trie trie, String title, String fileName){

        System.out.println(title);

        Array<String> array = new Array<>();

        FileHelper.readFile(fileName,array);

        System.out.println("Total words : "+array.getSize());

        long startTime = System.nanoTime();
        for (int i=0;i<array.getSize();i++) {
            trie.add(array.get(i));
        }
        long endTime = System.nanoTime();

        System.out.println(String.format("Total different words : custom = %d",trie.getSize()));

        return (endTime-startTime)/1000000000.0;
    }
}
